package com.txzt.gaea.base.controller;

import com.txzt.base.constant.RtnConstant;
import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.base.dto.DictInfoQueryDTO;
import com.txzt.gaea.base.model.DictInfo;
import com.txzt.gaea.base.service.DictInfoService;
import com.txzt.gaea.base.vo.DictInfoVo;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.page.PageInfoBT;
import com.txzt.gaea.common.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/dict/info")
public class DictInfoController extends BaseController {

    @Autowired
    private DictInfoService dictInfoService;

    @PostMapping(value = "/list")
    public RtnInfo<PageInfoBT<DictInfoVo>> pageList(@RequestBody DictInfoQueryDTO dictInfoQueryDTO){
        if (!StringUtil.isEmpty(dictInfoQueryDTO.getStartDate())){
            dictInfoQueryDTO.setStartDate(dictInfoQueryDTO.getStartDate() + " 00:00:00");
        }
        if (!StringUtil.isEmpty(dictInfoQueryDTO.getEndDate())){
            dictInfoQueryDTO.setEndDate(dictInfoQueryDTO.getEndDate() + " 23:59:59");
        }
        return dictInfoService.list(dictInfoQueryDTO);
    }

    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo add(@RequestBody DictInfo dictInfo){
        if (null != dictInfo && null == dictInfo.getId()){
            //查重
            if (!dictInfoService.addCheckCode(dictInfo)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查字典编码");
            }
            //序号自动+1
//            dictInfo.setSort(dictInfoService.getLastSort()+1);
            int num = dictInfoService.save(dictInfo);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据添加失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/getLastSort")
    public RtnInfo getLastSort() {
        return RtnInfo.success(dictInfoService.getLastSort());
    }

    @GetMapping(value = "/item/{id}")
    public RtnInfo getInfoById(@PathVariable Long id){
        return dictInfoService.getDictInfo(id);
    }

    @PostMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo edit(@RequestBody DictInfo dictInfo){
        if (null != dictInfo && null != dictInfo.getId()){
            //查重
            if (!dictInfoService.updateCheckCode(dictInfo)){
                return RtnInfo.error(RtnConstant.Code.PARAMS_REEOR,"存在重复数据，请检查字典编码");
            }
            int num = dictInfoService.update(dictInfo);
            if (0 == num) {
                return RtnInfo.error(RtnConstant.Code.SERVER_ERROR_CODE,"数据更新失败，请联系管理员");
            }else{
                return RtnInfo.success(num);
            }
        }else {
            return RtnInfo.PARAM_MISSING;
        }
    }

    @GetMapping(value = "/delete/{id}")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo remove(@PathVariable Long id){
        return RtnInfo.success(dictInfoService.delete(id));
    }

    @GetMapping(value = "/all")
    public RtnInfo getAllDictInfo(){
        return RtnInfo.success(dictInfoService.getAllDictInfo());
    }


}
